package YuanFuDao;

import domain.ListNode;

/**
 * 删除链表中的重复数字
 * 输入: 1 2 2 2 3 3 5 8
 * 输出: 1 5 8
 *
 * @author: leiwenkang
 * @date: 2020/5/31
 * @time: 14:12
 */
public class Test02移除重复节点2 {
    public static void main(String[] args) {

    }

    public static ListNode removeDupNode(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = head;

        ListNode a = dummy;
        ListNode b = head;
        while (b.next != null) {
            if (a.next.val != b.next.val) {
                a = a.next;
                b = b.next;
            } else {
                while (b != null && b.next != null && a.next.val == b.next.val) {
                    b = b.next;
                }
                a.next = b.next;
                b = b.next;
            }
        }
        return dummy.next;
    }
}
